package com.pethospital.service.impl;

import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pethospital.dto.StaffLoginDTO;
import com.pethospital.dto.StaffRegisterDTO;
import com.pethospital.entity.Staff;
import com.pethospital.exception.BusinessException;
import com.pethospital.mapper.StaffMapper;
import com.pethospital.service.StaffService;
import com.pethospital.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {

    @Resource
    private JwtUtil jwtUtil;

    @Override
    public boolean register(StaffRegisterDTO registerDTO) {
        // 检查手机号是否已注册
        if (count(new LambdaQueryWrapper<Staff>().eq(Staff::getPhone, registerDTO.getPhone())) > 0) {
            throw new BusinessException("该手机号已注册");
        }
        
        // 创建医护人员对象
        Staff staff = new Staff();
        BeanUtils.copyProperties(registerDTO, staff);
        
        // 密码加密
        staff.setPassword(BCrypt.hashpw(registerDTO.getPassword()));
        
        // 设置默认值
        staff.setCreateTime(LocalDateTime.now());
        staff.setUpdateTime(LocalDateTime.now());
        staff.setStatus(1);
        
        // 保存医护人员
        return save(staff);
    }

    @Override
    public Map<String, Object> login(StaffLoginDTO loginDTO) {
        // 查询医护人员
        Staff staff = getOne(new LambdaQueryWrapper<Staff>().eq(Staff::getPhone, loginDTO.getPhone()));
        
        // 检查医护人员是否存在
        if (staff == null) {
            throw new BusinessException("账号不存在");
        }
        
        // 检查密码是否正确
        if (!BCrypt.checkpw(loginDTO.getPassword(), staff.getPassword())) {
            throw new BusinessException("密码错误");
        }
        
        // 检查账号状态
        if (staff.getStatus() != 1) {
            throw new BusinessException("账号已被禁用");
        }
        
        // 生成token
        String token = jwtUtil.generateToken(staff.getId(), 2); // 2表示医护人员
        
        // 返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("staff", staff);
        
        return result;
    }

    @Override
    public Staff getById(Long id) {
        return super.getById(id);
    }

    @Override
    public boolean updateById(Staff staff) {
        staff.setUpdateTime(LocalDateTime.now());
        return super.updateById(staff);
    }

    @Override
    public Page<Staff> getStaffList(Long departmentId, int page, int size) {
        Page<Staff> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Staff> queryWrapper = new LambdaQueryWrapper<>();
        
        // 设置查询条件，只查询医生（角色为2）且状态为正常的医护人员
        queryWrapper.eq(Staff::getRole, 2).eq(Staff::getStatus, 1);
        
        // 如果科室ID不为空，则按科室ID过滤
        if (departmentId != null) {
            queryWrapper.eq(Staff::getDepartmentId, departmentId);
        }
        
        // 按创建时间降序排序
        queryWrapper.orderByDesc(Staff::getCreateTime);
        
        // 分页查询
        return page(pageParam, queryWrapper);
    }
} 